# update time: 2023-2-14 

#-------------------------------------------------------#
# 一般设置
#-------------------------------------------------------#

set -g mouse on # 启用鼠标
 
set -g base-index 1      # 窗口序号从0开始
set -g pane-base-index 1 # 窗格序号从0开始
 
# 支持utf8
set  -q -g status-utf8 on     # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on

# Or for tmux >= 2.6
set -sg escape-time 10

# 使用256色
set -g default-terminal "screen-256color"
set-option -ga terminal-overrides ",xterm-256color:Tc"

set-option -sa terminal-overrides ',screen-256color:RGB' # Segguested by neovim healthcheck
set-option -sa terminal-overrides ',XXX:RGB'

#-------------------------------------------------------#
# 风格设置
#-------------------------------------------------------#

# 窗口状态栏颜色
setw -g window-status-current-style bg='#484a4c'
setw -ga window-status-current-style fg=white
setw -ga window-status-current-style bright

set -g pane-border-style fg='#78909C' # 窗格边框颜色
set -g pane-active-border-style fg='#00FF2E' 

set-option -g status-justify left # 状态栏左对齐

set-option -g status-bg '#484a4c' #这是整个大状态栏的背景
set-option -g status-fg white # 字体前景色

set-option -g status-left-length 40
set-option -g status-right-length 80

# 消息框颜色
set -g  message-style fg=white
set -ga message-style bg=black
set -ga message-style bright

# 自动设置窗口标题
# set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # 窗口编号,程序名称,是否活动

# 通知方式
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off



#-------------------------------------------------------#
# 窗格管理
#-------------------------------------------------------#

unbind C-b               # 取消默认的前缀键 C-b
set-option -g prefix C-f # 设置新的前缀键 C-f

# 窗格分割快捷键
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# 使得vim中的分屏和tmux分屏使用同一个操作逻辑
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h'  'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k'  'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l'  'select-pane -R'

# smart pane switching with awareness of vim splits
# bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"
# bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-j) || tmux select-pane -D"
# bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-k) || tmux select-pane -U"
# bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-l) || tmux select-pane -R"

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R

# 使当前窗口和其他所有窗口形成左右或者上下的排布关系，ctrl+..
# bind + select-layout main-horizontal
# bind = select-layout main-vertical

bind -r o rotate-window # 使用prefix+o顺时针移动窗格的位置

set -g history-limit 10000 # 历史长度

# Enable native Mac OS X copy/paste
set-option -g default-command "/bin/sh -c 'which reattach-to-user-namespace >/dev/null && exec reattach-to-user-namespace $SHELL -l || exec $SHELL -l'"


#-------------------------------------------------------#
# vim模式
#-------------------------------------------------------#

# use vi mode
setw -g mode-keys vi
set  -g status-keys vi


# 使用 Vim 风格的按键在窗格间移动
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

bind [ copy-mode # 进入复制模式

# v进入选择模式，y复制
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind -T copy-mode-vi Enter send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"
